<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * @param $path 日志路径
 * @param $level 日志记录级别
 * 核心函数与类
 */
class Log{
    protected $path;
    public $level;
    function __construct(){
        //1.error  2.warning 3.notes

        $config = get_file_contents('./config.php');
        print_r($config);die;

        $this->level = 1;
        $this->path = ROOT_PATH.'/Log';
        if(!is_dir($this->path)){
            if(!mkdir($this->path,true)){
                echo '日志启动失败，没有创建日志目录权限!';
            }
            //chmod($this->path, 0777);
        }
        chmod($this->path,0777);
        //echo $this->path;
    }



    public function setLevel($level){
        if($level){$this->level = $level;}
        echo $level;
    }
    public function write($str,$level = 1){
        echo '已进入日志函数，检查是否写入';
        if($str == ''){return null;}
        date_default_timezone_set('Asia/Chongqing');
        $dirname = $this->path.'/'.date("Y-m-d",time()).'.txt';

        if($level > $this->level){return null;}
        if(file_exists($dirname)){

        }
        $fp = fopen($dirname,'a+');
        chmod($dirname,0666);
        $level_str = 'error';
        switch ($level){
            case 1:$level_str = 'error';break;
            case 2:$level_str = 'warning';break;
            case 3:$level_str = 'notes';break;
        }
        fwrite($fp,date('Y:m:d H:i:s',time()).'----'.$level_str."\n");
        fwrite($fp,$str."\n");
        return null;
    }
}